/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.servico;

import br.com.foxinline.modelo.Curso;
import br.com.foxinline.modelo.Disciplina;
import br.com.foxinline.modelo.Docente;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author laverson
 */
@Stateless
public class DocenteServico extends ServicoGenerico<Docente> {

    public DocenteServico() {
        super(Docente.class);
    }

    public List<Docente> pesquisar(Docente entity, Curso curso) {

        String sql = "Select d from Docente d ";

        List<Disciplina> disciplinas = entity.getDisciplinas();

        if (!disciplinas.isEmpty() || (curso != null && curso.getId() != null)) {
            sql += "JOIN d.disciplinas dis ";
        }

        sql += " where ";

        if (entity.getSituacao() != null) {
            sql += " lower(d.situacao) like lower(:situacao) AND ";
        }
        if (entity.getNome() != null && !entity.getNome().equals("")) {
            sql += " lower(d.nome) like lower(:nome) AND ";
        }
        if (entity.getArea() != null && !entity.getArea().equals("")) {
            sql += " lower(d.area) like lower(:area) AND ";
        }

        if (!disciplinas.isEmpty()) {
            sql += " dis.nome in :dis AND ";
        }

        if (curso != null && curso.getId() != null) {
            sql += " dis.curso = :curso AND ";
        }

        sql += " 1=1 ";

        Query query = getEntityManager().createQuery(sql);

        if (entity.getSituacao() != null) {
            query.setParameter("dituacao", "%" + entity.getSituacao() + "%");
        }
        if (entity.getNome() != null && !entity.getNome().equals("")) {
            query.setParameter("nome", "%" + entity.getNome().replace(" ", "%") + "%");
        }
        if (entity.getArea() != null && !entity.getArea().equals("")) {
            query.setParameter("area", "%" + entity.getArea().replace(" ", "%") + "%");
        }

        if (!disciplinas.isEmpty()) {
            List<String> dis = new ArrayList<String>();

            for (Disciplina disciplina : disciplinas) {
                dis.add(disciplina.getNome());
            }

            query.setParameter("dis", dis);
        }

        if (curso != null && curso.getId() != null) {
            query.setParameter("curso", curso);
        }

        return query.getResultList();
    }

    public List<Docente> pesquisar(Disciplina disciplina, Curso curso) {

        String sql = "SELECT d FROM Docente d JOIN d.disciplinas dis WHERE ";

        if (disciplina != null && disciplina.getId() != null) {
            sql += " dis = :dis AND ";
        }
        
        if (curso != null && curso.getId() != null) {
            sql += " dis.curso = :curso AND ";
        }

        sql += " 1=1 ";
        
        Query query = getEntityManager().createQuery(sql);

        if (disciplina != null && disciplina.getId() != null) {
            query.setParameter("dis", disciplina);
        }
        
        if (curso != null && curso.getId() != null) {
            query.setParameter("curso", curso);
        }

        return query.getResultList();
    }
}
